home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Format / ascii2fax / Xfont2PPfont.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  4.5 KB  |  225 lines

  1. /* xfont_conv.c:convert an X font to PP nonX format */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Format/ascii2fax/RCS/Xfont2PPfont.c,v 6.0 1991/12/18 20:15:19 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Format/ascii2fax/RCS/Xfont2PPfont.c,v 6.0 1991/12/18 20:15:19 jpo Rel $
  9.  *
  10.  * $Log: Xfont2PPfont.c,v $
  11.  * Revision 6.0  1991/12/18  20:15:19  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15. #include    "util.h"
  16. #include     <stdio.h>
  17. #include     <X11/Xlib.h>
  18. #include     <X11/Xutil.h>
  19. #include        <X11/Intrinsic.h>
  20. #include        <X11/StringDefs.h>
  21. #include        <X11/Shell.h>
  22. #include    "fonts.h"
  23. #include    <isode/cmd_srch.h>
  24.  
  25. #define    OPT_FONT    1
  26. #define OPT_FILE    2
  27.  
  28. CMD_TABLE    tbl_options [] = {
  29.     "-font",    OPT_FONT,
  30.     "-file",    OPT_FILE,
  31.     0,        -1,
  32. };
  33.  
  34. extern PPFontPtr    new_font();
  35. extern CharPtr        new_char();
  36. extern BitMap        new_bitmap();
  37. #define    MAXASCII    127
  38.  
  39. typedef struct _cmdLineResources {
  40.         char    *font;
  41.     char    *file;
  42. } CmdLineResources;
  43.  
  44. static CmdLineResources cmdLine_resources;
  45.  
  46. clear_pixmap(display, pixmap, gc,wd,ht)
  47. Display    *display;
  48. Pixmap    pixmap;
  49. GC    gc;
  50. unsigned int    wd, ht;
  51. {
  52.     /* clear the pixmap */
  53.     XSetFunction(display, gc, GXclear);
  54.  
  55.     XFillRectangle(display,
  56.            pixmap,
  57.            gc,
  58.            0, 0,
  59.            wd,ht);
  60.  
  61.     XSetFunction(display, gc, GXcopy);
  62. }
  63.  
  64. main(argc, argv)
  65. int    argc;
  66. char    **argv;
  67. {      
  68.     Arg     arg[1];
  69.     XFontStruct    *fontstruct;
  70.     Display    *display;
  71.     XtAppContext    appContext;
  72.     PPFontPtr    ppfont;
  73.     FILE        *fp;
  74.     int    wid, ht, base, i;
  75.     char    ch;
  76.     Pixmap    pixmap;
  77.     XGCValues    gcv;
  78.     GC        gc;
  79.     unsigned long    black, white;
  80.     XImage    *image;
  81.     int    row, col;
  82.  
  83.     for (i = 1; i < argc; i++) {
  84.         switch (cmd_srch(argv[i], tbl_options)) {
  85.             case OPT_FILE:
  86.             if (++i >= argc) {
  87.                 printf("No file name given with flag '%s'\n",
  88.                        argv[i-1]);
  89.                 exit (1);
  90.             }
  91.             cmdLine_resources.file = strdup(argv[i]);
  92.             break;
  93.             case OPT_FONT:
  94.             if (++i >= argc) {
  95.                 printf("No font name given with flag '%s'\n",
  96.                        argv[i-1]);
  97.                 exit (1);
  98.             }
  99.             cmdLine_resources.font = strdup(argv[i]);
  100.             break;
  101.             default:
  102.             printf("Unknown flag '%s'\n",
  103.                    argv[i]);
  104.             exit(1);
  105.         }
  106.     }
  107.  
  108.     if (cmdLine_resources.font == NULLCP) {
  109.         printf("No font specified\n");
  110.         exit(0);
  111.     }
  112.  
  113.     XtToolkitInitialize();
  114.  
  115.         appContext = XtCreateApplicationContext();
  116.  
  117.         display = XtOpenDisplay(appContext,
  118.                 (String) NULL,
  119.                 (String) argv[0],
  120.                 (String) "PPCONVERTERS",
  121.                 NULL, 0,
  122.                 &argc,
  123.                 argv);
  124.  
  125.     if (display == NULL) {
  126.                 printf("%s\n", "unable to open display");
  127.                 exit(1);
  128.         }
  129.     
  130.  
  131.     black = XBlackPixel(display, XDefaultScreen(display));
  132.     white = XWhitePixel(display, XDefaultScreen(display));
  133.  
  134.     if ((fontstruct = XLoadQueryFont(display, 
  135.                      cmdLine_resources.font)) == NULL) {
  136.         printf("display %s doesn't know about font '%s'\n",
  137.                DisplayString(display), cmdLine_resources.font);
  138.         exit(1);
  139.     }
  140.     
  141.     ht = fontstruct->max_bounds.ascent 
  142.         + fontstruct->max_bounds.descent;
  143.     wid = fontstruct->max_bounds.lbearing
  144.         + fontstruct->max_bounds.rbearing;
  145.     base = fontstruct->max_bounds.ascent;
  146.  
  147.     pixmap = XCreatePixmap(display,
  148.                    XRootWindow(display, XDefaultScreen(display)),
  149.                    wid, ht,
  150.                    (unsigned int)1);
  151.  
  152.  
  153.     gcv.font = fontstruct->fid;
  154.  
  155.     gcv.foreground = black;
  156.     gcv.background = white;
  157.  
  158.     gc = XCreateGC(display, pixmap, 
  159.                (GCFont | GCForeground |GCBackground), &gcv);
  160.  
  161.     ppfont = new_font(MAXASCII+1);
  162.  
  163.     for (i = 0;i < MAXASCII+1;i++) {
  164.         /* step through ascii codes */
  165.         CharPtr    temp = new_char();
  166.         temp->ascii = i;
  167.         
  168.         if (isprint(ch = (char) i)) {
  169.             char    buf[1];
  170.             buf[0] = ch;
  171.  
  172.             clear_pixmap(display, pixmap, gc, 
  173.                      wid, ht);
  174.             
  175.             temp->wid = XTextWidth(fontstruct,
  176.                            buf,
  177.                            1);
  178.             temp->ht = ht;
  179.  
  180.             XDrawImageString(display,
  181.                      pixmap,
  182.                      gc,
  183.                      0,
  184.                      base,
  185.                      buf,
  186.                      1);
  187.  
  188.             image = XGetImage(display,
  189.                       pixmap,
  190.                       0, 0,
  191.                       temp->wid, temp->ht,
  192.                       XAllPlanes(),
  193.                       XYPixmap);
  194.             
  195.             /* convert x bitmap into pp bitmap */
  196.             temp->bits = new_bitmap(temp->wid, temp->ht);
  197.  
  198.             for (row = 0; row < temp->ht; row++) {
  199.                 for (col = 0; col < temp->wid; col++) {
  200.                     if (XGetPixel(image, col, row) == white) 
  201.                         BL_CLR(col, temp->bits[row]);
  202.                     else
  203.                         BL_SET(col, temp->bits[row]);
  204.                 }
  205.             }
  206.         }
  207.         ppfont->chars[i] = temp;
  208.     }
  209.     
  210.     if (cmdLine_resources.file) {
  211.         if ((fp = fopen(cmdLine_resources.file, "w")) == NULL) {
  212.             printf("unable to open output file '%s'",
  213.                    cmdLine_resources.file);
  214.             exit (1);
  215.         }
  216.     } else
  217.         fp = stdout;
  218.  
  219.     font2file (fp, ppfont);
  220.  
  221.     if (fp != stdout)
  222.         fclose(fp);
  223.     exit(0);
  224. }
  225.